package com.kapo.egroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kapo.egroup.entity.base.project.Project;
import com.kapo.egroup.entity.base.project.UserApplyRecord;
import com.kapo.egroup.entity.base.project.UserInviteRecord;
import com.kapo.egroup.entity.base.project.UserProjectRecord;
import com.kapo.egroup.entity.base.user.User;
import com.kapo.egroup.mapper.*;
import com.kapo.egroup.service.ApplyService;
import com.kapo.egroup.service.InviteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title ApplyServiceImpl
 * @Description TODO
 * @Author 86159
 * @Date 2023-04-01 00:28
 **/
@Service
public class ApplyServiceImpl extends ServiceImpl<UserApplyRecordMapper, UserApplyRecord> implements ApplyService {
    @Autowired
    UserApplyRecordMapper userApplyRecordMapper;

    @Autowired
    UserProjectRecordMapper userProjectRecordMapper;

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    UserLessonRecordMapper userLessonRecordMapper;

    @Override
    public void applyProjectByUserId(Long projectId, Long receiverId, Long senderId,String description,String outline) {
        UserApplyRecord userApplyRecord = new UserApplyRecord();
        userApplyRecord.setProjectId(projectId);
        userApplyRecord.setSenderId(senderId);
        userApplyRecord.setReceiverId(receiverId);
        userApplyRecord.setDescription(description);
        userApplyRecord.setState(0);
        userApplyRecord.setWechatExchangeState(0);
        userApplyRecord.setOutline(outline);
        baseMapper.insert(userApplyRecord);
    }

    @Override
    public void acceptApplyById(Long applyRecordId) {
        UserApplyRecord userApplyRecord = userApplyRecordMapper.selectById(applyRecordId);
        userApplyRecord.setState(1);
        QueryWrapper<UserApplyRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("_id",applyRecordId);
        baseMapper.update(userApplyRecord,queryWrapper);
        //  添加申请人到项目组中
        Long applicantId = userApplyRecord.getSenderId();
        UserProjectRecord userProjectRecord = new UserProjectRecord();
        userProjectRecord.setUserId(applicantId);
        userProjectRecord.setProjectId(userApplyRecord.getProjectId());
        userProjectRecordMapper.insert(userProjectRecord);
    }

    @Override
    public void rejectApplyById(Long applyRecordId) {
        UserApplyRecord userApplyRecord = userApplyRecordMapper.selectById(applyRecordId);
        userApplyRecord.setState(-1);
        QueryWrapper<UserApplyRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("_id",applyRecordId);
        baseMapper.update(userApplyRecord,queryWrapper);
    }

    @Override
    public List<UserApplyRecord> getApplyByUserId(Long userId) {
        QueryWrapper<UserApplyRecord> queryWrapperReceiver = new QueryWrapper<>();
        QueryWrapper<UserApplyRecord> queryWrapperSender = new QueryWrapper<>();
        queryWrapperReceiver.eq("receiver_id",userId);
        queryWrapperSender.eq("sender_id",userId);
        List<UserApplyRecord> userApplyRecordReceiverList = baseMapper.selectList(queryWrapperReceiver);
        List<UserApplyRecord> userApplyRecordSenderList = baseMapper.selectList(queryWrapperSender);
        List<UserApplyRecord> resList = new ArrayList<>(userApplyRecordSenderList);
        resList.addAll(userApplyRecordReceiverList);
        return resList;
    }
}
